/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.edu.kamai.media.audio;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
 *
 * @author xyzhu
 */
public class wavPlayer extends Thread{
    public wavPlayer(String fn){
        this.fileName = fn;
        isComplete = false;
    }
    
    public void init() throws UnsupportedAudioFileException{
        try{
            AudioInputStream stream = AudioSystem.getAudioInputStream(
                    new File(fileName));
            format = stream.getFormat();
            samples = getSamples(stream);
            
        }catch(UnsupportedAudioFileException e){
            throw e;
        }catch(Exception ex){
            ex.printStackTrace();
        }
       
    }
    
    private byte[] getSamples(AudioInputStream audioStream) {
        int length = (int)(audioStream.getFrameLength() * format.getFrameSize());
        byte[] samples = new byte[length];
        DataInputStream is = new DataInputStream(audioStream);
        try {
            is.readFully(samples);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return samples;
    }
    
    private byte[] getSamples() {
        return samples;
    }
    
    public boolean isFinished(){
        return isComplete;
    }
    
    private void play(){
        InputStream source =new ByteArrayInputStream(getSamples());
        int bufferSize = format.getFrameSize() *
            Math.round(format.getSampleRate() / 10);
        byte[] buffer = new byte[bufferSize];
        
        try {
            isComplete = false;
            DataLine.Info info =
                new DataLine.Info(SourceDataLine.class, format);
            line = (SourceDataLine)AudioSystem.getLine(info);
            line.open(format, bufferSize);
        }
        catch (LineUnavailableException ex) {
            ex.printStackTrace();
        }
        line.start();
        try {
            int numBytesRead = 0;
            while (numBytesRead != -1) {
                numBytesRead =
                    source.read(buffer, 0, buffer.length);
                if (numBytesRead != -1) {
                   line.write(buffer, 0, numBytesRead);
                }
            }
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
        isComplete = true;
        line.drain();
        line.close();
    }

    @Override
    public void run() {
        play();
    }
    
    public void close(){
        line.stop();
    }
    
    private boolean isComplete;
    private String fileName;
    private AudioFormat format;
    private byte[] samples;
    SourceDataLine line = null;
    public static void main(String args[]){
        //String filename = "C:\\Program Files\\Hewlett-Packard\\HP Setup\\Assets\\goButton_initial.wav";
        String filename = "C:\\Users\\user\\Desktop\\无损煲耳机\\1 You Are Everything.wav";
        filename = "C:\\Users\\user\\kamai\\87c854d67de5069021fc824bde909e02.wma";
        wavPlayer wp = new wavPlayer(filename);
        try{
            wp.init();
        }catch(UnsupportedAudioFileException ex){
            
        }
        wp.start();
    }
}
